package com.wanmait.baohan.controller.personnelController;

import com.github.pagehelper.PageInfo;
import com.wanmait.baohan.pojo.Department;
import com.wanmait.baohan.pojo.Post;
import com.wanmait.baohan.pojo.Staff;
import com.wanmait.baohan.service.DepartmentService;
import com.wanmait.baohan.service.PostService;
import com.wanmait.baohan.service.StaffService;
import com.wanmait.baohan.vo.DataVo;
import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("personal")
public class StaffContorller {
    @Resource
    private PostService postService;
    @Resource
    private StaffService staffService;
    @Resource
    private DepartmentService departmentService;
    /**
     * 高文强
     * 跳转到显示所有人员的名单
     */
    @RequestMapping("personnel")
    public  void personnel(Model model)
    {
        List<Staff> staff = staffService.selectAllName();
        List<Post> posts = postService.selectAll();
        List<Department> departments = departmentService.selectAll();
        model.addAttribute("posts", posts);
        model.addAttribute("departments",departments);
        model.addAttribute("staffs", staff);
    }

    /**
     * 高文强
     * 跳转到显示所有人员的名单
     */
    @RequestMapping("toData")
    @ResponseBody
    public DataVo toData(Integer page, Integer limit,String name,String department,String post)
    {

        DataVo  dataVo = new DataVo();
        PageInfo<Staff> allStaff = staffService.findAllStaff(page, limit,name,department,post);
        List<Staff> list = allStaff.getList();
        int i =(page - 1) * limit;
        int j =page * limit;
        if(j>list.size()){
            j=(page-1)*limit+(list.size()%limit);
        }
        List<Staff> staff = list.subList(i , j);
        dataVo.setData(staff);
        dataVo.setCount((long)list.size());
        return dataVo;
    }
    /**
     * 王森明
     * 跳转到个人详情页面
     */

    @RequestMapping("persondetail")
    public ModelAndView persondetail(Integer id)
    {
        ModelAndView modelAndView =new ModelAndView();

        Staff staff = staffService.findOneById(id);

        modelAndView.addObject("staff",staff);
        return modelAndView;
    }
    /**
     * 王森明
     * 跳转到个人资料修改页面
     */
    @RequestMapping("personInfoDetail")
    public ModelAndView personInfoDetail(HttpServletRequest request)
    {
        ModelAndView modelAndView =new ModelAndView();
        HttpSession session =request.getSession();
        Staff s =(Staff) session.getAttribute("user");

        Staff staff = staffService.findOneById(s.getId());
        modelAndView.addObject("staff",staff);
        return modelAndView;
    }

    /**
     * 王森明
     * 个人资料修改
     */
    @RequestMapping("personInfoChange")
    public String personInfoChange(Staff staff, MultipartFile pictures,HttpServletRequest request)
    {

        HttpSession session =request.getSession();
        Staff staff1 =(Staff) session.getAttribute("user");
        staff.setId(staff1.getId());

        if(pictures.getOriginalFilename().length()!=0) {
            String ext = FilenameUtils.getExtension(pictures.getOriginalFilename());

            if (",jpg,png,gif,bmp,jpeg,webp,".contains("," + ext + ",")) {

                try {
                    //唯一文件名
                    String fileName = UUID.randomUUID() + "." + ext;
                    //物理路径
                    String path = ResourceUtils.getURL("classpath:").getPath()+"static/image/headers";//request.getServletContext().getRealPath("/static/image");

                    File file = new File(path, fileName);
                    pictures.transferTo(file);

                    staff.setPicture(fileName);

                } catch (IOException e) {
                    e.printStackTrace();
                }
                staffService.updateOne(staff);
                return "redirect:/personal/personInfoDetail?msg=0";
            }

            return "redirect:/personal/personInfoDetail?msg=1";
        }

        staffService.updateOne(staff);
        return "redirect:/personal/personInfoDetail?msg=0";
    }


    /**
     * 王森明
     * 修改密码
     */
    @RequestMapping("changepass")
    @ResponseBody
    public void changepass(HttpServletRequest request,String value)
    {
        HttpSession session =request.getSession();
        Staff staff =(Staff) session.getAttribute("user");
        staff.setPassword(value);
        staffService.updateOne(staff);
    }

    /**
     * hxn
     * @return
     * 获得全部员工
     */
    @RequestMapping("findAllStaff")
    @ResponseBody
    public List<Staff> findAllStaff(){
        return staffService.findAllStaff();
    }
}
